package com.pany.utils;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;

import play.db.jpa.JPABase;

public class ModelUtil
{
    public static <T> T queryField(String hql, Object... params)
    {
        List<T> list = queryFieldList(hql, params);
        if (CollectionUtils.isEmpty(list))
        {
            return null;
        }
        return list.get(0);
    }

    public static <T> List<T> queryFieldList(String hql, Object... params)
    {
        Query q = JPABase.em().createQuery(hql);
        if (!ArrayUtils.isEmpty(params))
        {
            for (int i = 0; i < params.length; i++)
            {
                q.setParameter(i + 1, params[i]);
            }
        }
        return q.getResultList();
    }
}
